package cn.sc.summer.token.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.sc.summer.token.encrypt.properties.EncryptProperties;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import cn.sc.summer.constant.util.AssertUtil;
import cn.sc.summer.constant.util.HttpUtil;
import cn.sc.summer.redis.util.NacosInstancesService;
import cn.sc.summer.token.service.EncryptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类名：服务间访问工具类，使用http形式进行通信
 *
 * @author a-xin
 * @date 2024/6/29 14:38
 */
@Slf4j
@Component
public class ServerHttpUtil {

    @Resource
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    @Resource
    private EncryptProperties encryptProperties;

    @Resource
    private EncryptService encryptService;

    /**
     * POST，服务间请求调用，服务间调用采用feign形式，会进行加解密
     *
     * @param serverName 服务名称
     * @param url        访问地址
     * @param header     请求头信息
     * @param params     参数信息
     * @return 访问结果
     */
    public String get(String serverName, String url, Map<String, Object> header, Map<String, Object> params) {
        List<String> allInstance = NacosInstancesService.getAllInstance(serverName, nacosDiscoveryProperties);
        if (CollUtil.isEmpty(allInstance)) {
            throw new RuntimeException("The user information cannot be verified, please check the " + serverName + " service！");
        }

        Map<String, List<String>> headers = HeaderUtil.getFeignHeaderList();
        HttpRequest request = HttpRequest.get("http://" + allInstance.get(0) + url + HttpUtil.creatGetParam(params)).header(headers);
        AssertUtil.functionIfPre(header, CollUtil::isNotEmpty, () -> header.forEach((key, value) -> request.header(key, value.toString())));

        HttpResponse execute = request.execute();
        String body = execute.body();
        if (encryptProperties.getEncryptFeign()) {
            return encryptService.decryptFeignData(body);
        }
        return body;
    }

    /**
     * POST，服务间请求调用，服务间调用采用feign形式，会进行加解密
     *
     * @param serverName 服务名称
     * @param url        访问地址
     * @param header     请求头信息
     * @param params     参数信息
     * @return 访问结果
     */
    public String post(String serverName, String url, Map<String, Object> header, Map<String, Object> params) {
        List<String> allInstance = NacosInstancesService.getAllInstance(serverName, nacosDiscoveryProperties);
        if (CollUtil.isEmpty(allInstance)) {
            throw new RuntimeException("The user information cannot be verified, please check the " + serverName + " service！");
        }

        Map<String, List<String>> headers = HeaderUtil.getFeignHeaderList();
        headers.put(HttpHeaders.CONTENT_TYPE, ListUtil.of(MediaType.APPLICATION_JSON_VALUE));
        HttpRequest request = HttpRequest.post("http://" + allInstance.get(0) + url).header(headers);
        AssertUtil.functionIfPre(header, CollUtil::isNotEmpty, () -> header.forEach((key, value) -> request.header(key, ObjectUtil.isNotNull(value) ? value.toString() : "")));
        AssertUtil.functionIfPre(params, CollUtil::isNotEmpty, () -> params.forEach(request::form));

        HttpResponse execute = request.execute();
        String body = execute.body();
        if (encryptProperties.getEncryptFeign()) {
            return encryptService.decryptFeignData(body);
        }
        return body;
    }




}
